package com.ostrich.library_logger

import com.ostrich.library_logger.logger.*

/**
 * @Author : yaotianxue
 * @Time : On 2023/10/20 10:47
 * @Description : Logger 根据用户设置策略创建不同的logger对象
 */
//主构造私有化 private constructor()
class Logger private constructor(private val tag:String,private val isDebug:Boolean,private val level: LoggerLevel,private val url: String,private val loggerStrategy:LoggerStrategy){
    private var logger: ILogger //真正的logger对象根据不同策略创建不同的logger对象
    init {
        logger = getLogger()
    }
    //策略者模式
    private fun getLogger(): ILogger {
        logger = when(loggerStrategy){
            LoggerStrategy.LOGCAT ->  LogCatLogger()
            LoggerStrategy.EMAIL ->  EmailLogger()
            LoggerStrategy.NET ->  NetWorkLogger()
            LoggerStrategy.FILE ->  FileLogger()
        }
        logger.setLoggerLevel(level)
        logger.setLogTag(tag)
        logger.setSaveUrl(url)
        logger.setDebug(isDebug)
        return logger
    }


    fun d(tag:String,log:String){
        logger.d(tag,log)
    }
    fun v(tag:String,log:String){
        logger.v(tag,log)
    }
    fun i(tag:String,log:String){
        logger.i(tag,log)
    }
    fun w(tag:String,log:String){
        logger.w(tag,log)
    }
    fun e(tag:String,log:String){
        logger.d(tag,log)
    }












    //次构造必须继承主构造
    constructor(builder: Builder):this(
        builder.tag,
        builder.isDebug,
        builder.level,
        builder.url,
        builder.loggerStrategy
    )


    //构建者模式
    class Builder{
         var isDebug: Boolean = true
         var tag:String = ""
         var level: LoggerLevel = LoggerLevel.Debug
         var url: String = ""
         var loggerStrategy:LoggerStrategy = LoggerStrategy.LOGCAT  //日志输出策略

        fun setDebug(isDebug: Boolean): Builder {
            this.isDebug = isDebug
            return this
        }

        fun setTag(tag:String): Builder {
            this.tag = tag
            return this
        }
        fun setLevel(level: LoggerLevel ): Builder {
            this.level = level
            return this
        }

        fun setSaveUrl(url: String ): Builder {
            this.url = url
            return this
        }

        fun setStrategy(loggerStrategy:LoggerStrategy ): Builder {
            this.loggerStrategy = loggerStrategy
            return this
        }
        //构建
        fun build(): Logger {
            return Logger(this)
        }


    }





}